<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm :searchFunc="searchFunc" :resetFunc="onReset">
        <a-form-item label="" class="table-search-item">
          <!-- <a-icon slot="suffixIcon" type="sync" /> -->
          <JeepayDateRangePicker ref="dateRangePicker" v-model:value="vdata.searchData['queryDateRange']" customDateRangeType="dateTime" />
        </a-form-item>
        <jeepay-text-up v-model:value="vdata.searchData.divisionRecordId" placeholder="分账记录ID" />
        <jeepay-text-up v-model:value="vdata.searchData.receiverId" placeholder="分账接受者ID" />
        <jeepay-text-up v-model:value="vdata.searchData.receiverGroupId" placeholder="分账账号组ID" />
        <jeepay-text-up v-model:value="vdata.searchData.appId" placeholder="应用AppId" />
        <jeepay-text-up v-model:value="vdata.searchData.payOrderId" placeholder="支付订单号" />
        <jeepay-text-up v-model:value="vdata.searchData.payOrderRefundOrderId" placeholder="退款订单号" />
        <jeepay-text-up v-model:value="vdata.searchData.accNo" placeholder="分账接收账号" />

        <a-form-item label="" class="table-search-item">
          <a-select v-model:value="vdata.searchData.state" placeholder="退款状态">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="0">待退款</a-select-option>
            <a-select-option value="1">分账成功</a-select-option>
            <a-select-option value="2">分账失败</a-select-option>
          </a-select>
        </a-form-item>
      </JeepaySearchForm>

      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :initData="true"
        :reqTableDataFunc="reqTableDataFunc"
        :tableColumns="tableColumns"
        :searchData="vdata.searchData"
        rowKey="recordId"
        @btnLoadClose="vdata.btnLoading=false"
      >
        <template #bodyCell="{column,record}">
          <template v-if="column.key == 'divisionAmount'"><b>￥{{ record.divisionAmount/100 }}</b></template>
          <template v-if="column.key == 'divisionRefundAmount'"><b>￥{{ record.divisionRefundAmount/100 }}</b></template>
          <template v-if="column.key == 'payOrderAmount'">￥{{ record.payOrderAmount/100 }}</template>
          <template v-if="column.key == 'state'">
            <a-tag v-if="record.state == 0" color="orange">回退中</a-tag>
            <a-tag v-if="record.state == 1" color="blue">回退成功</a-tag>
            <a-tag v-if="record.state == 2" color="volcano">回退失败</a-tag>
          </template>
          <template v-if="column.key == 'ifCode'">
            <template v-for="(item) in vdata.ifCodeList" :key="item.ifCode">
              <label v-if="record.ifCode == item.ifCode">{{ item.ifName }}({{ item.ifCode }})</label>
            </template>
          </template>
          <template v-if="column.key == 'op'">
            <!-- 操作列插槽 -->
            <a-button v-if="$access('ENT_DIVISION_REFUND_RECORD_VIEW')" type="link" @click="detailFunc(record.divisionRefundId)">详情</a-button>
          </template>
          <template v-if="column.key == 'payOrderDivisionAmount'">{{ (record.payOrderDivisionAmount / 100).toFixed(2) }}</template>
          <template v-if="column.key == 'divisionProfit'">{{ (record.divisionProfit * 100).toFixed(2) }}%</template>
        </template>
      </JeepayTable>
    </a-card>

    <Detail ref="recordDetail" />
  </page-header-wrapper>
</template>
<script lang="ts" setup>
import { API_URL_PAY_ORDER_DIVISION_REFUND_RECORD_LIST, API_URL_IFDEFINES_LIST, req } from '@/api/manage'
import Detail from './Detail.vue'
import { reactive, ref,getCurrentInstance, onMounted } from 'vue'

const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties
// eslint-disable-next-line no-unused-vars

const tableColumns = reactive([
  
  { key: 'divisionRefundId', title: '分账退款订单号', dataIndex: 'divisionRefundId' },
  { key: 'divisionRefundAmount', title: '回退金额' },
  { key: 'divisionAmount', title: '分账金额（首次）' },
  { key: 'divisionRecordId', title: '分账记录ID', dataIndex: 'divisionRecordId' },
  { key: 'payOrderId', title: '支付订单号', dataIndex: 'payOrderId' },
  { key: 'payOrderRefundOrderId', title: '支付订单退款单号', dataIndex: 'payOrderRefundOrderId' },
  { key: 'ifCode', title: '支付接口' },
  { key: 'payOrderAmount', title: '支付订单金额' },
  { key: 'receiverAlias', title: '账号别名', dataIndex: 'receiverAlias' },
  { key: 'accNo', title: '接收账号', dataIndex: 'accNo' },
  { key: 'accName', title: '账号姓名', dataIndex: 'accName' },
  { key: 'relationTypeName', title: '分账关系类型', dataIndex: 'relationTypeName' },
  { key: 'divisionProfit', dataIndex: 'divisionProfit', title: '分账比例'},
  { key: 'state', title: '状态', scopedSlots: { customRender: 'stateSlot' } },
  { key: 'createdAt', dataIndex: 'createdAt', title: '创建日期' },
  { key: 'op', title: '操作', width: '180px', fixed: 'right', align: 'center' }
])

const recordDetail = ref()
const infoTable = ref()
const dateRangePicker = ref()

const vdata = reactive({
  btnLoading: false,
  searchData: {} as any,
  ifCodeList: [] as any,
  visible:false,
})

onMounted(() => {
  if($access('ENT_PC_IF_DEFINE_LIST')){
    req.list(API_URL_IFDEFINES_LIST, {state:1}).then((res) => {
        vdata.ifCodeList = res
      })
    }
})

// 请求table接口数据
function reqTableDataFunc(params){
    return req.list(API_URL_PAY_ORDER_DIVISION_REFUND_RECORD_LIST, params)
}

function searchFunc() { // 点击【查询】按钮点击事件
    infoTable.value.refTable(true)
}

function detailFunc (recordId) {
    recordDetail.value.show(recordId)
}

function onReset(){
  //重置搜索内容
  dateRangePicker.value.returnSelectModel()
  vdata.searchData = { queryDateRange: '' }
}
</script>
